/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.house.web;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.util.BeanJsonUtil;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.ICoWorkerService;
import com.ce.pms.house.vo.CoWorkerVO;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：协作伙伴的ACTION超类,本类已完成基本功能,实现类中实现setCoWorkerVoInfoForSysrefcode方法即可
 * @author 罗军林 创建日期：2008-7-2
 */
public abstract class CoWorkerAction extends PmsBaseAction{

	protected ICoWorkerService coWorkerService;

	/**
	 * 功能描述:初始化协作伙伴的主页面
	 *
	 * Create author:罗军林<br>
	 * Create on:2008-7-2<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initCoworkerMain(){

	}

	/**
	 * 功能描述:初始化协作伙伴查询列表
	 *
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void initCoWorkerList(){
		setAttribute("totalRows", new Integer(0));
	}

	/**
	 * 功能描述:初始化协作伙伴主页面
	 *
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void initCoWorkerMain(){

	}

	/**
	 * 功能描述:初始化协作伙伴新增页面
	 *
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void initCoWorkerAdd(){
		setAttribute("isEdit",false );
	}

	/**
	 * 功能描述:初始化协作伙伴修改页面
	 *
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void initCoWorkerEdit(@Read(key="coWorkerId") Long coWorkerID){
		setAttribute("coWorker", coWorkerService.findCoWorkerByID(coWorkerID));
		setAttribute("isEdit",true );
	}

	/**
	 * 功能描述:增加一个协作伙伴
	 * @param coWorkerVO
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void addCoWorker(@Read CoWorkerVO coWorkerVO){
		coWorkerVO.setCreator(this.getUserId());
		setCoWorkerVoInfoForSysrefcode(coWorkerVO);
		coWorkerService.addCoWorker(coWorkerVO);

	}

	/**
	 * 功能描述:修改一个协作伙伴
	 * @param coWorkerVO
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void editCoWorker(@Read CoWorkerVO coWorkerVO){
		coWorkerVO.setCreator(this.getUserId());
		setCoWorkerVoInfoForSysrefcode(coWorkerVO);
		coWorkerService.editCoWorker(coWorkerVO);
	}

	/**
	 * 功能描述:删除一个协作伙伴
	 * @param coWorkerId
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void deleteCoWorker(@Read(key="coWorkerId") Long coWorkerId){
		try{
			coWorkerService.deleteCoWorkerByID(coWorkerId);
		}catch (Exception e) {
			throw new BusinessException("common:delete_error_exception",BusinessException.WARNING);
		}
	}


	/**
	 * 功能描述:根据查询条件进行查询
	 * @param pageSize
	 * @param pageNo
	 * @param queryCondition
	 * Create author:罗军林<br>
	 * Create on:2008-7-2<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void listCoWorker(	@Read(key = "ec_crd",defaultValue="15") int pageSize,
										@Read(key = "ec_p",defaultValue="1")	int pageNo,
										@Read CoWorkerVO queryCondition){
		setCoWorkerVoInfoForSysrefcode(queryCondition);
		Page page=coWorkerService.queryCoWorker(queryCondition, pageNo, pageSize);

		setAttribute("coWorkerVOList", page.getResult());
		setAttribute("totalRows", new Integer((int) page.getTotalCount()));

	}

	/**
	 * 功能描述:验证name的唯一性,这里仅在一个页签中的唯一性
	 * @param name
	 * Create author:罗军林<br>
	 * Create on:2008-7-3<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void checkCoworkByID(@Read(key = "name") String name,
								@Read(key = "oldName") String oldName){
		CoWorkerVO queryCondition=new CoWorkerVO();
		queryCondition.setName(name);
		setCoWorkerVoInfoForSysrefcode(queryCondition);
		Long result=coWorkerService.countCoWorkerByName(queryCondition,oldName);
		if(result<=0){
			renderText("true");
		}else{
			renderText("已经存在");
		}
	}

	public void findCoWorkerById(@Read(key="coWorkerId") Long coWorkerId) throws Exception{
		CoWorkerVO queryCondition=new CoWorkerVO();
		setCoWorkerVoInfoForSysrefcode(queryCondition);
		CoWorkerVO result=coWorkerService.findCoWorkerByID(coWorkerId);

		String jsonStr=BeanJsonUtil.BeanToJsonStr(result);
		renderText(jsonStr);
	}

	/************私有方法*****************************************/

	/** 功能描述:将协作伙伴的信息写入coWorkerVO中,在子类中实现本类;<br>
	 * 写入了四个值：OrganizationTypeCode,OrganizationTypeValue,CoworkerTypeCode,CoworkerTypeValue
	 * @param coWorkerVO
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	protected abstract void setCoWorkerVoInfoForSysrefcode(CoWorkerVO coWorkerVO);



	/*****get/set方法********************************************/

	/**
	 * @return the coWorkerService
	 */
	public ICoWorkerService getCoWorkerService() {
		return coWorkerService;
	}

	/**
	 * @param coWorkerService the coWorkerService to set
	 */
	public void setCoWorkerService(ICoWorkerService coWorkerService) {
		this.coWorkerService = coWorkerService;
	}



}
